<template>
    <div class="page bg-white">
      <el-row>
        <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建一级机构信息</el-button>
      </el-row>
    <el-treetable
      :data="dataList"
      isTreeTable
      size="medium"
      v-loading = "loading"
      row-key="id"
      class="table treetable">
    <el-treetable-column
      label="序号"
    type="index">
    </el-treetable-column>
    <el-treetable-column
        prop="organization"
        align="left"
        show-overflow-tooltip
        label="机构名称">
      </el-treetable-column>
      <el-treetable-column
          prop="organizationLevel"
          align="left"
          show-overflow-tooltip
          label="机构类别">
        </el-treetable-column>
        <el-treetable-column
            prop="code"
            align="left"
            show-overflow-tooltip
            label="编码">
          </el-treetable-column>
        <el-treetable-column
            prop="special"
            align="left"
            show-overflow-tooltip
            label="人员特殊处理">
          </el-treetable-column>
      <el-treetable-column
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="300"
        label="操作">
        <template  slot-scope="scope">
          <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:view')" type="text" size="small" v-show="scope.row.parentId == 0" icon="el-icon-view" @click="viewFirst(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:view')" type="text" size="small" v-show="scope.row.parentId != 0" icon="el-icon-view" @click="viewSecond(scope.row.id)">查看</el-button>
          <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:edit')" type="text" size="small" v-show="scope.row.parentId == 0" icon="el-icon-edit" @click="editFirst(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:edit')" type="text" size="small" v-show="scope.row.parentId != 0" icon="el-icon-edit" @click="editSecond(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:del')" type="text" size="small" icon="el-icon-delete" @click="del(scope.row.id)">删除</el-button>
          <el-button v-if="hasPermission('peopleinformation:organization:nameOfOrganization:add')" type="text" size="small" v-show="scope.row.parentId == 0" icon="el-icon-circle-plus-outline" @click="addChild(scope.row.id, scope.row.name, scope.row.organizationLevel)">添加二级机构信息</el-button>
        </template>
      </el-treetable-column>
    </el-treetable>
        <!-- 弹窗, 新增 / 修改 -->
    <NameOfOrganizationForm  ref="nameOfOrganizationForm" @refreshDataList="refreshList"></NameOfOrganizationForm>
  </div>
</template>

<script>
  import NameOfOrganizationForm from './NameOfOrganizationForm'
  export default {
    data () {
      return {
        dataList: [],
        loading: false
      }
    },
    components: {
      NameOfOrganizationForm
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.$http({
          url: '/peopleinformation/organization/nameOfOrganization/treeData',
          method: 'get'
        }).then(({data}) => {
          this.dataList = data.treeData
          this.loading = false
        })
      },
        // 新增下级
      addChild (id, name, organizationLevel) {
        console.log(organizationLevel)
        this.$refs.nameOfOrganizationForm.init('addChild', {id: '', parent: {id: id, name: name, organizationLevel: organizationLevel}})
      },
       // 新增
      add () {
        var number = 0
        if (this.dataList[this.dataList.length - 1] !== undefined) {
          number = this.dataList[this.dataList.length - 1].code
        }
        this.$refs.nameOfOrganizationForm.init('add', {id: '', parent: {id: '', name: ''}}, number)
      },
      // 修改
      editFirst (id) {
        this.$refs.nameOfOrganizationForm.init('editFirst', {id: id, parent: {id: '', name: ''}})
      },
      editSecond (id) {
        this.$refs.nameOfOrganizationForm.init('editSecond', {id: id, parent: {id: '', name: ''}})
      },
      // 查看
      viewFirst (id) {
        this.$refs.nameOfOrganizationForm.init('viewFirst', {id: id, parent: {id: '', name: ''}})
      },
      viewSecond (id) {
        this.$refs.nameOfOrganizationForm.init('viewSecond', {id: id, parent: {id: '', name: ''}})
      },
      // 删除
      del (id) {
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/peopleinformation/organization/nameOfOrganization/delete',
            method: 'delete',
            params: {'id': id}
          }).then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.msg)
              this.refreshList()
            }
            this.loading = false
          })
        })
      }
    }
  }
</script>
